package com.example.guiguihomemedical.mapper;

import com.example.guiguihomemedical.entity.Examination;
import com.example.guiguihomemedical.entity.ExaminationTable;
import com.example.guiguihomemedical.entity.Record;
import com.example.guiguihomemedical.entity.Signing;
import com.example.guiguihomemedical.entity.dto.ExaminationRecord;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface ExaminationMapper {
    @Insert("INSERT INTO examination (idcard, id, pulserate, respiratoryrate, leftpressure, rightpressure, weight, " +
            "waist, mbi, oldhealthy, oldselfcare, oldcognition, oldemotion, diet, exercise, sleep, drink, work, lip, " +
            "dentalcaries, pharynx, lefteye, righteye, hearing, motion, fundus, skin, urineprotein, urinarysugar, " +
            "urinaryketonebody, urinaryoccultblood, urinaryother, fastingbloodglucose, electrocardiogram, " +
            "diagnosticresults, microurineprotein, fecaloccultblood, glycosylatedhemoglobin, hbsag, " +
            "serumalanineaminotransferase, serumaspartateaminotransferase, albumin, totalbilirubin, symptom, " +
            "temperature,starttime,height,kongfuxuetang) " +
            "VALUES (#{idcard}, #{id}, #{pulseRate}, #{respiratoryRate}, #{leftPressure}, #{rightPressure}, " +
            "#{weight}, #{waist}, #{mbi}, #{oldHealthy}, #{oldSelfCare}, #{oldCognition}, #{oldEmotion}, #{diet}, " +
            "#{exercise}, #{sleep}, #{drink}, #{work}, #{lip}, #{dentalCaries}, #{pharynx}, #{leftEye}, #{rightEye}, " +
            "#{hearing}, #{motion}, #{fundus}, #{skin}, #{urineProtein}, #{urinarySugar}, #{urinaryKetoneBody}, " +
            "#{urinaryOccultBlood}, #{urinaryOther}, #{fastingBloodGlucose}, #{electrocardiogram}, " +
            "#{diagnosticResults}, #{microUrineProtein}, #{fecalOccultBlood}, #{glycosylatedHemoglobin}, #{hbsag}, " +
            "#{serumAlanineAminotransferase}, #{serumAspartateAminotransferase}, #{albumin}, #{totalBilirubin}, " +
            "#{symptom}, #{temperature},#{starttime},#{height},#{kongfuxuetang})")
    void insertExamination(Examination examination);

    @Select("SELECT r.* FROM record r LEFT JOIN signing s ON r.idcard = s.idcard WHERE s.idcard IS NOT NULL AND " +
            "CURRENT_TIMESTAMP BETWEEN s.starttime AND s.endtime")
    List<Record> getExaminationSigningList();

    @Select("select lc.name from userclassification uc inner join listenerclustering lc on uc.peopleid = lc.id where " +
            "idcard=#{idcard}")
    List<String> getListenerNamesById(String idcard);

    @Select("select * from examination where examinationId=#{examinationId}")
    Examination getExaminationDetailByIdcard(Integer examinationId);

    @Select("select  ex.examinationid,ex.starttime,rc.idcard,rc.name,rc.sex  from record rc inner join examination ex" +
            " on rc" +
            ".idcard = ex.idcard ")
    List<ExaminationRecord> getExaminationRecordList();

    void modifyExaminationById(Examination examination);

    @Select("select * from signing where idcard=#{idcard}")
    List<Signing> getAddStatus(String idcard);

    @Insert("insert into examinationtable (examinationid, examinationtable) VALUES (#{examinationid}," +
            "#{examinationtable})")
    void insertTable(ExaminationTable examinationTable);

    @Update("update examinationtable set examinationtable=#{examinationtable} where examinationid=#{examinationid}")
    void updateTable(ExaminationTable examinationTable);

    @Select("select * from examinationtable where examinationid=#{examinationid}")
    ExaminationTable getExaminationTable(Integer examinationid);
}
